﻿/*************************************************************************************
* 运行库版本：	4.0.30319.18408
* 类  名  称：	CameraBase
* 命 名 空间：	FutureTech.Camera
* 文  件  名：	BitsWide
* 创 建 时间：	2014/3/23 12:14:57
* 作      者：	帅红  shuaihong617@qq.com
* 说      明：
* 修 改 时间：
* 修  改  人：
*************************************************************************************/

using System;
using System.Runtime.InteropServices;
using FutureTech.Media.Image2D;

namespace FutureTech.Hardware.Camera
{
        public abstract class FtCamera
        {
                protected FtCamera(string name, int horizontalResolution, int verticalResolution, BitsWide bitsWide, bool isSnapBmpFileHeader)
                {
                        Name = name;

                        Width = horizontalResolution;
                        Height = verticalResolution;

                        BitsWide = bitsWide;
                        IsSnapBmpFileHeader = isSnapBmpFileHeader;

                        PixelBufferBytesCount = Width*Height*(int)BitsWide/8;
                        if (IsSnapBmpFileHeader)
                        {
                                SnapBufferBytesCount = PixelBufferBytesCount + BmpFile.FileHeaderBytesCount +
                                                       BmpFile.InfoHeaderBytesCount;
                        }

                        SnapBufferPtr = Marshal.AllocHGlobal(SnapBufferBytesCount);

                        if (IsSnapBmpFileHeader)
                        {
                                PixelBufferPtr = SnapBufferPtr + BmpFile.FileHeaderBytesCount +
                                                 BmpFile.InfoHeaderBytesCount;
                        }
                        else
                        {
                                PixelBufferPtr = SnapBufferPtr;
                        }
                }

                #region 属性

                /// <summary>
                /// 名称
                /// </summary>
                public string Name { get; set; }

                /// <summary>
                /// 横向分辨率
                /// </summary>
                public int Width { get; private set; }

                /// <summary>
                /// 纵向分辨率
                /// </summary>
                public int Height { get; private set; }

                /// <summary>
                /// 采集位宽
                /// </summary>
                public BitsWide BitsWide { get;private set; }

                /// <summary>
                /// 采集数据是否包含BMP文件头
                /// </summary>
                public bool IsSnapBmpFileHeader { get; private set; }

                /// <summary>
                /// 采集缓冲区字节计数
                /// </summary>
                public int SnapBufferBytesCount { get; private set; }

                /// <summary>
                /// 采集缓冲区指针
                /// </summary>
                public IntPtr SnapBufferPtr { get;private set; }

                /// <summary>
                /// 像素缓冲区指针
                /// </summary>
                public IntPtr PixelBufferPtr { get;private set; }

                /// <summary>
                /// 像素缓冲区字节计数
                /// </summary>
                public int PixelBufferBytesCount { get; private set; }

                /// <summary>
                /// 像素缓冲区行宽
                /// </summary>
                public uint PixelBufferStride
                {
                        get { return (uint) (Width*(int) BitsWide/8); }
                }

                #endregion

                #region 事件

                public event EventHandler<MessageEventArgs> OpenFailed;

                

                public event EventHandler<MessageEventArgs> StartSnapFailed;

                public event EventHandler<MessageEventArgs> SnapFailed;

                public event EventHandler<MessageEventArgs> SnapSuccessed;

                

                public event EventHandler<MessageEventArgs> StopSnapFailed;

                public event EventHandler<MessageEventArgs> CloseFailed;

                #endregion



                #region 方法

                /// <summary>
                /// 打开摄像机
                /// </summary>
                public abstract void Open();

                /// <summary>
                /// 关闭摄像机
                /// </summary>
                public abstract void Close();

                /// <summary>
                /// 开始采集
                /// </summary>
                public abstract void StartSnap();


                /// <summary>
                /// 采集
                /// </summary>
                public abstract void Snap();

                /// <summary>
                /// 结束采集
                /// </summary>
                public abstract void StopSnap();


                #region 事件

                protected virtual void OnOpenFailed(MessageEventArgs e)
                {
                        e.Raise(this, ref OpenFailed);
                }

                protected virtual void OnSnapSuccessed(MessageEventArgs e)
                {
                        e.Raise(this, ref SnapSuccessed);
                }

                #endregion


                #endregion

        }
}
